<template>
  <div class="updateName-container">
    <van-nav-bar title="设置昵称"
    left-text="取消"
    right-text="完成"
    @click-left="$emit('close')"
    @click-right="onComfirm"
    >

    </van-nav-bar>
    <!-- 输入框 -->
    <div class="field-wrap">
      <van-field
      v-model="localName"
      rows="2"
      autosize

      type="textarea"
      maxlength="10"
      placeholder="请输入昵称"
      show-word-limit
    />
    </div>
    <!-- 输入框 -->

  </div>
</template>

<script>
import { updataUserProfile } from '@/api/user'
export default {
  name: 'UpdateName',
  props: {
    value: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },

  mounted () {

  },

  methods: {
    async onComfirm () {
      try {
        this.$toast.loading({
          message: '保存中...',
          forbidClick: true, // 禁用背景点击
          duration: 0 // 如果为0，则持续展示

        })
        const localName = this.localName
        if (!localName.length) {
          this.$toast('昵称不能为空')
        }
        await updataUserProfile({
          name: localName
        })
        // 更新视图
        this.$emit('input', localName)
        this.$emit('close')
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.field-wrap {
  padding: 10px;
}
.nav-bar {
  background-color: lightblue;
}
</style>
